package com.cdpm.system.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdpm.system.domain.SysArticle;
import com.cdpm.system.domain.vo.SysArticleRes;
import com.cdpm.system.domain.vo.SysArticleUpVO;
import com.cdpm.system.domain.vo.SysArticleVO;
import com.cdpm.system.mapper.SysArticleMapper;
import com.cdpm.system.mapper.SysArticleRecordMapper;
import com.cdpm.system.service.ISysArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class SysArticleServiceImpl extends ServiceImpl<SysArticleMapper,SysArticle> implements ISysArticleService{

    @Autowired
    private SysArticleMapper sysArticleMapper;
    @Autowired
    private SysArticleRecordMapper sysArticleRecordMapper;

    @Override
    public List<SysArticleRes> findList(SysArticleVO sysArticleVO) {

        return sysArticleMapper.findList(sysArticleVO);
    }

    @Override
    public SysArticle findById(Long id) {
        SysArticle sysArticle = getById(id);
        return sysArticle;
    }

    @Override
    public void insertSysArticle(SysArticle sysArticle) {
        sysArticle.setArticleId(null);
        sysArticle.setStatus("2");
        sysArticle.setCreateTime(new Date());
        log.info("新增文章内容{}",JSONObject.toJSONString(sysArticle));
        this.save(sysArticle);
    }

    @Override
    public void update(SysArticle sysArticle) {
        boolean b = super.updateById(sysArticle);
    }

    @Override
    public void closeOrUp(SysArticleUpVO sysArticleUpVO) {
        List<Long> articleIds = sysArticleUpVO.getArticleIds();
        String status = sysArticleUpVO.getStatus();
        log.info("文章上下架入参{}", JSONObject.toJSONString(sysArticleUpVO));
        if(articleIds==null|| articleIds.isEmpty()){
            throw new RuntimeException("请选择至少一条数据!");
        }
        List<SysArticle> articles=new ArrayList<>();
        for (Long id: articleIds){
            SysArticle sysArticle=new SysArticle();
            sysArticle.setArticleId(id);
            sysArticle.setStatus(status);
            articles.add(sysArticle);
        }
        this.updateBatchById(articles);
    }
}
